<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/3/26
 * 描述:
 */

namespace app\result\goods;

use app\model\goods\GoodsCollect;
use app\result\BaseResult;
use app\result\GoodsResult;

class GoodsCollectResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new GoodsCollect();
    }

    public function action($user_id, $args): array
    {
        $goods_id = intval($args['goods_id'] ?? 0);
        if (!$goods_id) {
            return self::error_return('请选择商品');
        }
        if ($this->model->data_get(['goods_id' => $goods_id, 'user_id' => $user_id])) {
            $res = $this->model->data_del(['goods_id' => $goods_id, 'user_id' => $user_id]);
            return self::success_return('已取消收藏');
        } else {
            $res = $this->model->data_add([
                'goods_id' => $goods_id,
                'user_id' => $user_id,
            ]);
        }
        if (!$res) {
            return self::error_return('收藏失败');
        }
        return self::success_return('收藏成功');
    }

    public function list($user_id, $args): array
    {
        $page = intval($args['page'] ?? 1);
        $limit = intval($args['limit'] ?? 20);
        $order_by = trim($args['order_by'] ?? '');
        $keywords = $args['keywords'] ?? '';
        $order = 'a.id desc';
        if ($order_by === 'sales_desc') {
            $order = 'b.sales_num desc';
        } elseif ($order_by === 'sales_asc') {
            $order = 'b.sales_num asc';
        } elseif ($order_by === 'price_desc') {
            $order = 'b.goods_price desc';
        } elseif ($order_by === 'price_asc') {
            $order = 'b.goods_price asc';
        }
        $where = [['a.user_id', '=', $user_id]];
        if ($keywords) {
            $where[] = ['b.goods_name', 'like', '%' . $keywords . '%'];
        }
        $field = 'b.goods_id as item_id,b.goods_price,b.goods_name as name,b.goods_image as image,b.market_price,b.sales_num,b.shipping_fee,b.goods_type';
        $field .= ',a.created_at';
        $data = $this->model->alias('a')
            ->join('goods b', 'a.goods_id=b.goods_id', 'left')
            ->field($field)
            ->where($where)
            ->order($order)
            ->page($page, $limit)
            ->select();
        if ($data) {
            foreach ($data as &$v) {
                $v['is_collect'] = 1;
                $v['tags'] = GoodsResult::get_tags($v);
            }
            unset($v);
        }
        $lazyLoadConfig = [
            'duration' => 300,//触发加载的延迟时间，单位 ms（默认300）
            'threshold' => -300,//触发加载时的位置，见上方说明，单位 rpx（默认300）
        ];
        return self::success_return('success', [
            'lazyLoadConfig' => $lazyLoadConfig,
            'list' => $data
        ]);
    }
}
